package org.budo.warehouse.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.budo.support.dao.page.Page;
import org.budo.support.mvcs.Mvcs;
import org.budo.warehouse.logic.api.IDataNodeLogic;
import org.budo.warehouse.service.api.IDataNodeService;
import org.budo.warehouse.service.entity.DataNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lmw
 */
@Controller
public class DataNodeController {
    @Resource
    private IDataNodeService dataNodeService;

    @Resource
    private IDataNodeLogic dataNodeLogic;

    @ResponseBody
    @RequestMapping("mysql_nodes")
    public String mysql_nodes() {
        List<DataNode> dataNodes = dataNodeService.listMysqlNodes(Page.max());

        String html = "<table border=\"1\">";
        html += "<tr>";
        html += "<td>ID</td>";
        html += "<td>URL</td>";
        html += "<td>Description</td>";
        html += "<td>GRANTS</td>";
        html += "</tr>";

        for (DataNode dataNode : dataNodes) {
            html += "<tr>";
            html += "<td>" + dataNode.getId() + "</td>";
            html += "<td>" + dataNode.getUrl() + "</td>";
            html += "<td>" + dataNode.getDescription() + "</td>";
            html += "<td>" + dataNodeLogic.showGrants(dataNode) + "</td>";
            html += "</tr>";
        }

        html += "</table>";

        Mvcs.setContentType("text/html;charset=utf-8");
        return html;
    }
}